package Gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.xml.sax.SAXException;

import Database.Person;
import Database.Wanted;
import Database.XMLProvider;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class DeleleDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tfSearch;
	private JTextField tfRAction;
	private JTextField tfRqDe;
	private JTextField tfRqLetters;
	private JTextField tfRqNumber;
	private JTextField tfRqDate;
	private JTextField tfFirstName;
	private JTextField tfLastName;
	private JTextField tfDoB;
	private JTextField tfPoB;
	private JTextField tfNationality;
	private JTextField tfAddress;
	private Wanted wanted; 
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DeleleDialog dialog = new DeleleDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DeleleDialog() {
		setBounds(100, 100, 616, 309);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("Input PersonID or Wanted ID");
			label.setBounds(36, 15, 201, 14);
			contentPanel.add(label);
		}
		{
			tfSearch = new JTextField();
			tfSearch.setBounds(208, 12, 228, 20);
			tfSearch.setColumns(10);
			contentPanel.add(tfSearch);
		}
		{
			JButton btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String id = tfSearch.getText();
					XMLProvider.LoadXML();
					try {
						 wanted = XMLProvider.getWantedbyPersonID(id);
						if(wanted == null)
						{
							wanted = XMLProvider.getWantedbyWantedID(id);
						}
						if(wanted == null)
							JOptionPane.showMessageDialog(null, "Cannot find Wanted");
						else
						{
							Person person = XMLProvider.GetPersonbyID(wanted.getPersonID());
							tfAddress.setText(person.getAddress());
							tfDoB.setText(person.getDoB()); 
							tfFirstName.setText(person.getfirstName());
							tfLastName.setText(person.getlastName());
							tfNationality.setText(person.getNationality());
							tfPoB.setText(person.getPoB());
							
						
							switch (wanted.getRequireAction()){
							case 1:
								tfRAction.setText("Must be Arrested");
								break;
							case 2:
								tfRAction.setText("Notify to Check");
								break;
							case 3:
								tfRAction.setText("Prevent from Travelling");
								break;
							case 4:
								tfRAction.setText("Put on Probation");
								break;
							case 5:
								tfRAction.setText("Release");
								break;
						}
							
							tfRqDe.setText(wanted.getDepartmentName());
							tfRqLetters.setText(wanted.getDepartmentLetter());
							tfRqNumber.setText(wanted.getDepartmentNumber());
							tfRqDate.setText(wanted.getRequestDate());
						}
					} catch (XPathExpressionException
							| ParserConfigurationException | SAXException
							| IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			});
			btnSearch.setBounds(446, 11, 79, 23);
			contentPanel.add(btnSearch);
		}
		{
			JLabel label = new JLabel("Required Action");
			label.setBounds(10, 46, 110, 14);
			contentPanel.add(label);
		}
		{
			tfRAction = new JTextField();
			tfRAction.setEditable(false);
			tfRAction.setColumns(10);
			tfRAction.setBounds(111, 43, 164, 20);
			contentPanel.add(tfRAction);
		}
		{
			JLabel label = new JLabel("Requesting Department ");
			label.setBounds(10, 71, 159, 14);
			contentPanel.add(label);
		}
		{
			tfRqDe = new JTextField();
			tfRqDe.setEditable(false);
			tfRqDe.setColumns(10);
			tfRqDe.setBounds(10, 96, 265, 20);
			contentPanel.add(tfRqDe);
		}
		{
			JLabel label = new JLabel("Requesting department letters");
			label.setBounds(10, 124, 235, 14);
			contentPanel.add(label);
		}
		{
			tfRqLetters = new JTextField();
			tfRqLetters.setEditable(false);
			tfRqLetters.setColumns(10);
			tfRqLetters.setBounds(10, 140, 265, 20);
			contentPanel.add(tfRqLetters);
		}
		{
			JLabel label = new JLabel("Requesting department number");
			label.setBounds(10, 165, 235, 14);
			contentPanel.add(label);
		}
		{
			tfRqNumber = new JTextField();
			tfRqNumber.setEditable(false);
			tfRqNumber.setColumns(10);
			tfRqNumber.setBounds(10, 180, 265, 20);
			contentPanel.add(tfRqNumber);
		}
		{
			tfRqDate = new JTextField();
			tfRqDate.setEditable(false);
			tfRqDate.setColumns(10);
			tfRqDate.setBounds(111, 207, 164, 20);
			contentPanel.add(tfRqDate);
		}
		{
			JLabel label = new JLabel("Request Date");
			label.setBounds(10, 211, 86, 14);
			contentPanel.add(label);
		}
		{
			JPanel panel = new JPanel();
			panel.setLayout(null);
			panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			panel.setBounds(285, 43, 313, 161);
			contentPanel.add(panel);
			{
				JLabel label = new JLabel("Firstname");
				label.setBounds(10, 11, 59, 14);
				panel.add(label);
			}
			{
				tfFirstName = new JTextField();
				tfFirstName.setEditable(false);
				tfFirstName.setColumns(10);
				tfFirstName.setBounds(71, 8, 78, 20);
				panel.add(tfFirstName);
			}
			{
				JLabel label = new JLabel("Lastname");
				label.setBounds(159, 11, 59, 14);
				panel.add(label);
			}
			{
				tfLastName = new JTextField();
				tfLastName.setEditable(false);
				tfLastName.setColumns(10);
				tfLastName.setBounds(217, 8, 86, 20);
				panel.add(tfLastName);
			}
			{
				JLabel label = new JLabel("DoB");
				label.setBounds(10, 36, 46, 14);
				panel.add(label);
			}
			{
				tfDoB = new JTextField();
				tfDoB.setEditable(false);
				tfDoB.setColumns(10);
				tfDoB.setBounds(70, 33, 78, 20);
				panel.add(tfDoB);
			}
			{
				JLabel label = new JLabel("PoB");
				label.setBounds(10, 61, 46, 14);
				panel.add(label);
			}
			{
				tfPoB = new JTextField();
				tfPoB.setEditable(false);
				tfPoB.setColumns(10);
				tfPoB.setBounds(70, 58, 225, 20);
				panel.add(tfPoB);
			}
			{
				JLabel label = new JLabel("Nationality");
				label.setBounds(10, 86, 59, 14);
				panel.add(label);
			}
			{
				tfNationality = new JTextField();
				tfNationality.setEditable(false);
				tfNationality.setColumns(10);
				tfNationality.setBounds(70, 83, 165, 20);
				panel.add(tfNationality);
			}
			{
				JLabel label = new JLabel("Address");
				label.setBounds(10, 114, 59, 14);
				panel.add(label);
			}
			{
				tfAddress = new JTextField();
				tfAddress.setEditable(false);
				tfAddress.setColumns(10);
				tfAddress.setBounds(70, 111, 165, 20);
				panel.add(tfAddress);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						boolean delete = XMLProvider.deleteWanted(wanted);
						if(delete == true)
							JOptionPane.showMessageDialog(null, "Delete Success!");
						else
							JOptionPane.showMessageDialog(null, "Delete Fail!");
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
